package controllers

import (
	"github.com/astaxie/beego"
	"WhereToEat/models"
	"math/rand"  
    "time"
)

type WheretoeatController struct {
	beego.Controller
}


type restaurant struct
{
	Name string  //餐厅名称
	Indoor bool // 室内
	Chance  int  //概率  最高10 最低0 
}

func (c *WheretoeatController) Get() {
	c.Data["reslut"]=getWhereToEat()
	c.TplName = "restaurant.tpl"
}



func getWhereToEat()string {
	reslut:="" 
	var chance int
	list:=loadRestaurant();//所有餐厅
	newList := []restaurant{} //结合天气状况 可选择的餐厅
	wa,hasWaether:=models.GetWeather("苏州")
	
	reslut+="今日所有可选餐厅有:<br/>"
    if hasWaether{
		outType,msg:=models.IsRightForOut(wa)
		for _, l := range list {
			//fmt.Println(l.Name)
			reslut+=l.Name+"<br/>"
			if(l.Indoor==false){
				if(outType==0){
					continue
				}
				if(outType==1){
					l.Chance=5
				}
				if(outType==2){
					l.Chance=15
				}
			}
			chance+=l.Chance
			newList=append(newList,l)
		}
		reslut+="----------------------<br/>"
		reslut+="今日天气:"+wa.Type+","+wa.High+","+wa.Low+","+"空气质量："+wa.Quality+"<br/>"
		if outType==0{
			//fmt.Println(msg+",移除所有同程大厦外部的餐厅")
			reslut+=msg+",移除所有同程大厦外部的餐厅<br/>"
		}else if outType==1{
			//fmt.Println(msg+",降低所有同程大厦外部的餐厅的选中几率")
			reslut+=msg+",降低所有同程大厦外部的餐厅的选中几率<br/>"
		}else {
			//fmt.Println(msg+",提高所有同程大厦外部的餐厅的选中几率")
			reslut+=msg+",提高所有同程大厦外部的餐厅的选中几率<br/>"
		}

	
	}else {
		reslut+="----------------------<br/>"
		for _, l := range list {
			//fmt.Println(l.Name)
			reslut+=l.Name+"<br/>"
			chance+=l.Chance
			newList=append(newList,l)
		}
		reslut+="(未获取到天气，排除天气因素)<br/>"
	}

	rand:=getRandomNumber(1,chance,1)[0]
	chance=0;
	for _, l := range list {
		chance+=l.Chance
		if(rand<=chance){
			//fmt.Printf("所以~今天去%s吃午餐吧\n",l.Name)
			reslut+="所以~今天去"+l.Name+"吃午餐吧<br/>"
			return reslut;
		}
	}
	return  reslut;

}

//加载餐厅列表
func loadRestaurant()[]restaurant{
	restList :=[]restaurant{
		{Name:"同程大厦负1楼餐厅",Indoor :true,Chance :10,},
		{Name:"同程大厦1楼餐厅",Indoor :true,Chance :10,},
		{Name:"同程大厦2楼餐厅",Indoor :true,Chance :10,},
		{Name:"同程大厦全家超市的便当",Indoor :true,Chance :10,},
		{ Name:"大师傅菜饭",Indoor :false,Chance :10},
		{ Name:"黄山菜饭",Indoor :false,Chance :10},
		{ Name:"浏阳蒸菜",Indoor :false,Chance :10},
		{ Name:"黄焖鸡",Indoor :false,Chance :10},
		{ Name:"兰州拉面",Indoor :false,Chance :10},
		//{ Name:"重庆小面",Indoor :false,Chance :10},
	}
   return restList
}

//生成count个[start,end)结束的不重复的随机数  
func getRandomNumber(start int, end int, count int) []int {  
    //范围检查  
    if end < start || (end-start) < count {  
        return nil  
    }  
    //存放结果的slice  
    nums := make([]int, 0)  
    //随机数生成器，加入时间戳保证每次生成的随机数不一样  
    r := rand.New(rand.NewSource(time.Now().UnixNano()))  
    for len(nums) < count {  
        //生成随机数返回一个取值范围在[0,n)的伪随机int值，如果n<=0会panic  
        num := r.Intn((end - start + 1)) + start //注意，这里不保护n  
        //查重  
        exist := false  
        for _, v := range nums {  
            if v == num {  
                exist = true  
                break  
            }  
        }  
        if !exist {  
            nums = append(nums, num)  
        }  
    }  
    return nums  
} 